<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="客户号" prop="customerNo" >
        <a-input v-model="form.customerNo" placeholder="请输入客户号" />
      </a-form-model-item>
      <a-form-model-item label="客户名称" prop="customerName" >
        <a-input v-model="form.customerName" placeholder="请输入客户名称" />
      </a-form-model-item>
      <a-form-model-item label="客户来源" prop="customerSource" >
        <a-input v-model="form.customerSource" placeholder="请输入客户来源" />
      </a-form-model-item>
      <a-form-model-item label="客户电话" prop="customerPhone" >
        <a-input v-model="form.customerPhone" placeholder="请输入客户电话" />
      </a-form-model-item>
      <a-form-model-item label="客户手机号" prop="customerMobile" >
        <a-input v-model="form.customerMobile" placeholder="请输入客户手机号" />
      </a-form-model-item>
      <a-form-model-item label="客户成交状态" prop="customerDealStatus" >
      </a-form-model-item>
      <a-form-model-item label="客户状态" prop="customerStatus" >
      </a-form-model-item>
      <a-form-model-item label="客户所属行业" prop="customerIndustry" >
        <a-input v-model="form.customerIndustry" placeholder="请输入客户所属行业" />
      </a-form-model-item>
      <a-form-model-item label="客户级别" prop="customerLevel" >
        <a-input v-model="form.customerLevel" placeholder="请输入客户级别" />
      </a-form-model-item>
      <a-form-model-item label="客户最后跟进时间" prop="customerLastFollowTime" >
        <a-date-picker style="width: 100%" v-model="form.customerLastFollowTime" format="YYYY-MM-DD HH:mm:ss" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="客户最后跟进记录" prop="customerLastFollowRecord" >
        <a-input v-model="form.customerLastFollowRecord" placeholder="请输入内容" type="textarea" allow-clear />
      </a-form-model-item>
      <a-form-model-item label="客户地址" prop="address" >
        <a-input v-model="form.address" placeholder="请输入客户地址" />
      </a-form-model-item>
      <a-form-model-item label="经度" prop="lng" >
        <a-input v-model="form.lng" placeholder="请输入经度" />
      </a-form-model-item>
      <a-form-model-item label="维度" prop="lat" >
        <a-input v-model="form.lat" placeholder="请输入维度" />
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remarks" >
        <a-input v-model="form.remarks" placeholder="请输入内容" type="textarea" allow-clear />
      </a-form-model-item>
      <a-form-model-item label="网站" prop="website" >
        <a-input v-model="form.website" placeholder="请输入网站" />
      </a-form-model-item>
      <a-form-model-item label="是否删除" prop="ifDel" >
        <a-input v-model="form.ifDel" placeholder="请输入是否删除" />
      </a-form-model-item>
      <a-form-model-item label="创建人id" prop="createById" >
        <a-input v-model="form.createById" placeholder="请输入创建人id" />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getInfo, addInfo, updateInfo } from '@/api/customer/info'

export default {
  name: 'CreateForm',
  props: {
  },
  components: {
  },
  data () {
    return {
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        customerId: null,

        customerNo: null,

        customerName: null,

        customerSource: null,

        customerPhone: null,

        customerMobile: null,

        customerDealStatus: '0',

        customerStatus: '0',

        customerIndustry: null,

        customerLevel: null,

        customerLastFollowTime: null,

        customerLastFollowRecord: null,

        address: null,

        lng: null,

        lat: null,

        remarks: null,

        website: null,

        ifDel: null,

        createTime: null,

        createById: null,

        createBy: null,

        updateTime: null

      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        customerId: null,

        customerNo: null,

        customerName: null,

        customerSource: null,

        customerPhone: null,

        customerMobile: null,

        customerDealStatus: '0',

        customerStatus: '0',

        customerIndustry: null,

        customerLevel: null,

        customerLastFollowTime: null,

        customerLastFollowRecord: null,

        address: null,

        lng: null,

        lat: null,

        remarks: null,

        website: null,

        ifDel: null,

        createTime: null,

        createById: null,

        createBy: null,

        updateTime: null

      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const customerId = row ? row.customerId : ids
      getInfo(customerId).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.customerId !== undefined && this.form.customerId !== null) {
            updateInfo(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addInfo(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
